package com.zzyl.nursing.service.impl;

import java.util.*;

import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.framework.web.service.TokenService;
import com.zzyl.nursing.dto.UserLoginRequestDto;
import com.zzyl.nursing.service.WechatService;
import com.zzyl.nursing.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.FamilyMemberMapper;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 老人家属Service业务层处理
 *
 * @author zouguanghuan
 * @date 2025-10-27
 */
@Service
public class FamilyMemberServiceImpl extends ServiceImpl<FamilyMemberMapper, FamilyMember> implements IFamilyMemberService {
    @Autowired
    private FamilyMemberMapper familyMemberMapper;

    @Autowired
    private WechatService wechatService;


    //随机姓名
    private static List<String> DEFAULT_NICKNAME_PREFIX = List.of(
            "生活更美好",
            "大桔大利",
            "日富一日",
            "好柿开花",
            "柿柿如意",
            "一椰暴富",
            "大柚所为",
            "杨梅吐气",
            "天生荔枝"
    );
    @Autowired
    private TokenService tokenService;


    /**
     * 查询老人家属
     *
     * @param id 老人家属主键
     * @return 老人家属
     */
    @Override
    public FamilyMember selectFamilyMemberById(Long id) {
        return getById(id);
    }

    /**
     * 查询老人家属列表
     *
     * @param familyMember 老人家属
     * @return 老人家属
     */
    @Override
    public List<FamilyMember> selectFamilyMemberList(FamilyMember familyMember) {
        return familyMemberMapper.selectFamilyMemberList(familyMember);
    }

    /**
     * 新增老人家属
     *
     * @param familyMember 老人家属
     * @return 结果
     */
    @Override
    public int insertFamilyMember(FamilyMember familyMember) {
        return save(familyMember) ? 1 : 0;
    }

    /**
     * 修改老人家属
     *
     * @param familyMember 老人家属
     * @return 结果
     */
    @Override
    public int updateFamilyMember(FamilyMember familyMember) {
        return updateById(familyMember) ? 1 : 0;
    }

    /**
     * 批量删除老人家属
     *
     * @param ids 需要删除的老人家属主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除老人家属信息
     *
     * @param id 老人家属主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    /**
     * 小程序端家属登录
     *
     * @param dto
     * @return
     */
    @Override
    public LoginVo login(UserLoginRequestDto dto) {
        LoginVo loginVo = new LoginVo();
        //获取openid
        String openid = wechatService.getOpenId(dto.getCode());
        //获取手机号
        String phoneNumber = wechatService.getPhoneNumber(dto.getPhoneCode());
        //根据openid查找用户
        FamilyMember memberInDB = lambdaQuery().eq(FamilyMember::getOpenId, openid).one();
        // 创建一个保存的实体类
        FamilyMember newMember = new FamilyMember();
        // 设置要保存的手机号码
        newMember.setPhone(phoneNumber);
        //4. 判断用户是否存在
        if (memberInDB != null) {
            //5. 存在
            loginVo.setNickName(memberInDB.getName());
            newMember.setId(memberInDB.getId());
            //更新手机号
            if (!memberInDB.getPhone().equals(phoneNumber)) {
                updateById(newMember);
            }
            //6. 不存在,新增
        } else {
            Random r = new Random();
            newMember.setOpenId(openid);
            //随机名字 + 手机后四位尾号
            newMember.setName(DEFAULT_NICKNAME_PREFIX.get(r.nextInt(DEFAULT_NICKNAME_PREFIX.size())) + phoneNumber.substring(7));
            save(newMember);
            loginVo.setNickName(newMember.getName());
        }
        //7. 把用户id与nickname封装到token返回
        Map<String, Object> claims = new HashMap<>();
        claims.put("userId", newMember.getId());
        UserThreadLocal.set(newMember.getId());
        claims.put("nickName", loginVo.getNickName());
        String token = tokenService.createToken(claims);
        loginVo.setToken(token);
        return loginVo;
    }
}
